home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 216_02 / undos.c < prev    next >
Text File  |  1980-01-01  |  4KB  |  202 lines

  1. /*% cc -O -K -i % -o undos
  2.  *
  3.  * Undos - change DOS format files to Unix, etc.
  4.  */
  5. char ID[] =
  6.  "Undos Rev 11-09-86 (C)Copyright Omen Technology Inc All Rights Reserved\n";
  7. /*
  8.  * This program and documentation may be copied, used, or modified
  9.  *  by Professional-YAM and POWERCOMM licensees provided these notices are
  10.  * not removed.  Others may use this program for non-profit purposes only.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16.  
  17. #define LL 1024
  18. #define SUB 032
  19.  
  20. char Lbuf[LL];
  21. char *Progname;
  22. int Todos = 0;
  23. int Tocpm = 0;
  24. int Tomac = 0;
  25. int Unmac = 0;
  26. int Strip = 0;
  27. int Unparity = 0;
  28.  
  29. main(argc, argv)
  30. char **argv;
  31. {
  32.     Progname = *argv;
  33.     if (! strcmp(Progname, "tocpm"))
  34.         Todos = Tocpm = 1;
  35.     if (! strcmp(Progname, "todos"))
  36.         Todos = 1;
  37.     if (! strcmp(Progname, "unmac"))
  38.         Unmac = 1;
  39.     if (! strcmp(Progname, "tomac"))
  40.         Tomac = 1;
  41.     if (! strcmp(Progname, "unparity"))
  42.         Unparity = 1;
  43.  
  44.     if (! strcmp(argv[1], "-s")) {
  45.         ++Strip; --argc; ++argv;
  46.     }
  47.  
  48.  
  49.     if (argc<2 || *argv[1]== '-')
  50.         usage();
  51.     while (--argc >= 1)
  52.         chngfmt(*++argv);
  53.     exit(0);
  54. }
  55. usage()
  56. {
  57.     fprintf(stderr, ID);
  58.     fprintf(stderr, "Usage: {undos|tounix|todos|tocpm|unmac} [-s] file ...\n");
  59.     fprintf(stderr, "    -s Strip parity bit, ignore bytes < 007\n");
  60.     fprintf(stderr, "-or-    unparity file ...\n");
  61.     exit(1);
  62. }
  63.  
  64.  
  65. chngfmt(name)
  66. char *name;
  67. {
  68.     register c;
  69.     register char *p;
  70.     register n;
  71.     register long fpos;
  72.     struct stat st;
  73.     FILE *fin, *fout;
  74.     int linno = 0;
  75.     long ftell();
  76.     char *mktemp();
  77.     char outnam[64];
  78.  
  79.     if (stat(name, &st)) {
  80.         xperror(name); return;
  81.     }
  82.     if ((st.st_mode & S_IFMT) != S_IFREG) {
  83.         fprintf(stderr, "%s: %s is not a regular file\n", Progname, name);
  84.         return;
  85.     }
  86.     if ((fin = fopen(name, "r")) == NULL) {
  87.         xperror(name); return;
  88.     }
  89. #if 0
  90.     setvbuf(fin, _IOFBF, (char *)0, 0x4000);
  91. #endif
  92.     strcpy(outnam, "undosXXXXXX");
  93.     mktemp(outnam);
  94.     if ((fout = fopen(outnam, "w")) == NULL) {
  95.         xperror(outnam); exit(1);
  96.     }
  97.  
  98.     if (Unparity) {
  99.             while ((c = getc(fin)) != EOF)
  100.                 if (putc((c & 0177), fout) == EOF) {
  101.                     xperror(outnam); exit(1);
  102.                 }
  103.             goto closeit;
  104.     }
  105.     for (;;) {
  106.         ++linno;
  107.         for (p=Lbuf, n=LL; --n>0; ) {
  108. ignore:
  109.             if ((c = getc(fin)) == EOF)
  110.                 break;
  111.             if ( !c)
  112.                 goto ignore;
  113.             if (c < '\7' || (c & 0200)) {
  114.                 if (Strip) {
  115.                     if ((c &= 0177) < 7)
  116.                         goto ignore;
  117.                 } else
  118.                     goto thisbin; 
  119.             }
  120.             if (c == SUB)
  121.                 break;
  122.             if (c == '\r' && Unmac)
  123.                 c = '\n';
  124.             *p++ = c;
  125.             if (c == '\n')
  126.                 break;
  127.         }
  128.         *p = '\0';
  129.  
  130.         if (n == 0) {
  131.     thisbin:
  132.             if (n) {
  133.                 fprintf(stderr, "%s: %s is a binary file", Progname, name);
  134.                 fprintf(stderr, " line=%d char =%2X\n", linno, c);
  135.             } else
  136.                 fprintf(stderr, "%s: %s has no linefeeds: try unmac?\n", Progname, name);
  137.             fclose(fout);
  138.             unlink(outnam);
  139.             return;
  140.         }
  141.  
  142.         if (Todos) {
  143.             if (*--p == '\n' && p[-1] != '\r') {
  144.                 *p++ = '\r'; *p++ = '\n'; *p = 0;
  145.             }
  146.         } else if (Tomac) {
  147.             if (*--p == '\n') {
  148.                 if (p[-1] == '\r')
  149.                     --p;
  150.                 *p++ = '\r'; *p = 0;
  151.             }
  152.         } else {
  153.             if (*--p == '\n' && *--p == '\r') {
  154.                 *p++ = '\n'; *p = 0;
  155.             }
  156.         }
  157.         if (Lbuf[0] && fputs(Lbuf, fout) == EOF) {
  158.             xperror(outnam); exit(1);
  159.         }
  160.         switch (c) {
  161.         case EOF:
  162.             if (ferror(fin)) {
  163.                 xperror(name); exit(0200);
  164.             }
  165.         case SUB:
  166.             if (Tocpm) {
  167.                 fpos = ftell(fout);
  168.                 do {
  169.                     putc(SUB, fout);
  170.                 } while (++fpos & 127);
  171.             }
  172. closeit:
  173.             fclose(fout); fclose(fin);
  174.             if (st.st_nlink > 1) 
  175.                 sprintf(Lbuf, "cp %s %s", outnam, name);
  176.             else
  177.                 sprintf(Lbuf, "mv %s %s", outnam, name);
  178.             system(Lbuf);
  179.             utime(name, (struct utimbuf *) &st.st_atime);
  180.             if (st.st_nlink > 1) 
  181.                 unlink(outnam);
  182.             return;
  183.         }
  184.     }
  185. }
  186.  
  187. xperror(s)
  188. char *s;
  189. {
  190.     register char *p;
  191.     extern int sys_nerr;
  192.     extern char *sys_errlist[];
  193.     extern errno;
  194.  
  195.     if (errno >= sys_nerr)
  196.         p = "Gloryovsky: a New Error!";
  197.     else
  198.         p = sys_errlist[errno];
  199.     fprintf(stderr, "%s: %s: %s\n", Progname, s, p);
  200. }
  201.  
  202.